package com.cy.store.service.impl;

import com.cy.store.entity.Cart;
import com.cy.store.entity.CartVO;
import com.cy.store.repository.CartRepository;
import com.cy.store.service.ICartService;
import com.cy.store.service.IProductService;
import com.cy.store.service.ex.AccessDeniedException;
import com.cy.store.service.ex.CartNotFoundException;
import com.cy.store.service.ex.InsertException;
import com.cy.store.service.ex.UpdateException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Iterator;
import java.util.List;

@Service
public class CartServiceImpl implements ICartService {
    @Autowired
    private CartRepository cartRepository;

    @Autowired
    private IProductService productService;

    @Override
    public void addToCart(Integer uid, Integer pid, Integer amount, String username) {
        //查询购物测中有无同款
        Cart old_cart = cartRepository.findByUidAndPid(uid,pid);
        if(old_cart == null){
            //新增，设置新数据对象
            Cart new_cart = new Cart();
            new_cart.setUid(uid);
            new_cart.setPid(pid);
            new_cart.setNum(amount);
            new_cart.setPrice(productService.findById(pid).getPrice());     //获取价格
            new_cart.setCreatedUser(username);
            new_cart.setCreatedTime(new Date());
            new_cart.setModifiedUser(username);
            new_cart.setModifiedTime(new Date());
            //插入数据库
            if(cartRepository.save(new_cart) == null) throw new InsertException("插入商品数据时出现未知错误");
        }else {
            //递增
            old_cart.setNum(old_cart.getNum() + amount);
            old_cart.setCreatedUser(username);
            old_cart.setCreatedTime(new Date());
            old_cart.setModifiedUser(username);
            old_cart.setModifiedTime(new Date());
            if(cartRepository.save(old_cart) == null) throw new UpdateException("修改商品数量时出现未知错误");
        }
    }

    @Override
    public List<CartVO> getVOByUid(Integer uid) {
        return cartRepository.findCartVo(uid);
    }

    @Override
    public Integer addNum(Integer cid, Integer uid, String username) {
        //根据cid查询购物车数据，抛出CartNotFoundException
        Cart cart = cartRepository.findById(cid).orElseThrow(()->new CartNotFoundException("访问的购物车数据不存在"));

        //判断结果的uid是否一致
        if(!cart.getUid().equals(uid)) throw new AccessDeniedException("非法访问");

        //优化：检查商品剩余库存是否大于增加数量
        //商品数+1
        Integer num = cart.getNum()+1;
        cart.setNum(num);

        //修改并补全数据
        cart.setModifiedUser(username);
        cart.setModifiedTime(new Date());
        if(cartRepository.save(cart)==null) throw new InsertException("修改商品数量时出现未知错误");

        return num;
    }

    @Override
    public List<CartVO> getVOByCids(Integer uid, Integer[] cids) {
        List<CartVO> list = cartRepository.findVOByCids(cids);
        //检验结果集中属于登录用户的
        Iterator<CartVO> it = list.iterator();
        while (it.hasNext()) {
            CartVO cart = it.next();
            if (!cart.getUid().equals(uid)) {
                it.remove();
            }
        }
        return list;
    }
}
